<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="back-btn" @click="goBack">
        <text class="back-icon">&lt;</text>
      </view>
      <view class="nav-title">显示图片</view>
    </view>

    <!-- 圆形头像区域 -->
    <view class="avatar-wrapper">
      <image 
        class="avatar" 
        :src="imageUrl" 
        mode="aspectFill"
        @click="previewImage"
      ></image>
    </view>

    <!-- 功能按钮区域 -->
    <view class="button-group">
      <button class="action-btn" @click="chooseImage">选择图片显示</button>
      <button class="action-btn" @click="previewImage">预览图片</button>
      <button class="action-btn" @click="getImageInfo">获取图片信息</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: '/static/momo.png' // 默认图片路径
    };
  },
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack({
        delta: 1
      });
    },

    // 选择图片
    chooseImage() {
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          this.imageUrl = res.tempFilePaths[0];
        },
        fail: (err) => {
          console.error('选择图片失败：', err);
        }
      });
    },

    // 预览图片
    previewImage() {
      uni.previewImage({
        urls: [this.imageUrl],
        current: this.imageUrl,
        success: () => {
          console.log('预览图片成功');
        },
        fail: (err) => {
          console.error('预览图片失败：', err);
        }
      });
    },

    // 获取图片信息
    getImageInfo() {
      uni.getImageInfo({
        src: this.imageUrl,
        success: (info) => {
          uni.showModal({
            title: '图片信息',
            content: `宽：${info.width}px\n高：${info.height}px\n格式：${info.type}`,
            showCancel: false
          });
        },
        fail: (err) => {
          console.error('获取图片信息失败：', err);
        }
      });
    }
  }
};
</script>

<style scoped>
.container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-top: var(--status-bar-height);
}

/* 导航栏样式 */
.nav-bar {
  display: flex;
  align-items: center;
  height: 90rpx;
  background-color: #fff;
  padding: 0 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
}

.back-btn {
  width: 60rpx;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  font-size: 38rpx;
  color: #333;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 34rpx;
  font-weight: 600;
  color: #333;
}

/* 圆形头像样式 */
.avatar-wrapper {
  width: 240rpx;
  height: 240rpx;
  margin: 80rpx auto;
  border-radius: 50%;
  overflow: hidden;
  border: 6rpx solid #fff;
  box-shadow: 0 5rpx 20rpx rgba(0,0,0,0.1);
}

.avatar {
  width: 100%;
  height: 100%;
}

/* 按钮区域样式 */
.button-group {
  display: flex;
  flex-direction: column;
  padding: 0 40rpx;
  gap: 30rpx;
}

.action-btn {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 16rpx;
  background-color: #2c83ff;
  color: #fff;
  border: none;
}

/* 去除按钮默认边框 */
.action-btn::after {
  border: none;
}
</style>